import Vue from 'vue'


export function formatDateTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


/**
 * 计算两个日期之间的工作日（只排除周六日）
 * @param sDay 开始日期
 * @param eDay 结束日期
 * @returns {number}
 */
export function countWorkDay (sDay, eDay) {
  sDay = new Date(Date.parse(sDay.replace(/-/g, "/")));
  eDay = new Date(Date.parse(eDay.replace(/-/g, "/")));
  let s_t_w = sDay.getDay(), e_t_w = eDay.getDay();
  // 总相差天数
  let diffDay = (eDay - sDay) / (1000 * 60 * 60 * 24) + 1;
  if (parseInt(diffDay) == 0)
    return parseInt(diffDay);
  // 周末天数
  let weekEnds = 0;
  for (let i = 0; i < diffDay; i++) {
    if (sDay.getDay() == 0 || sDay.getDay() == 6)
      weekEnds++;
    sDay = sDay.valueOf();
    sDay += 1000 * 60 * 60 * 24;
    sDay = new Date(sDay);
  }
  return parseInt(diffDay - weekEnds);
}
